package com.shycloud.mido.task.job;

import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.music.feign.MusicRemoteService;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.shycloud.mido.common.core.constant.SecurityConstants;
import com.shycloud.mido.homework.api.feign.HomeworkFeignService;
import com.shycloud.mido.homework.api.feign.StudentFeignService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class WeekMainJob {

	@Autowired
	private HomeworkFeignService homeworkFeignService;
	@Autowired
	private StudentFeignService studentFeignService;
	@Autowired
	private MusicRemoteService musicRemoteService;

	@XxlJob("testExecute")
	public void testExecute() throws Exception {
		String param = XxlJobHelper.getJobParam();
		log.info("接收调度中心参数...[{}]", param);
		log.info("testExecute execute...");

		try {
			log.info("测试任务执行" + param + "秒");
			TimeUnit.SECONDS.sleep(Integer.valueOf(param));
			XxlJobHelper.log("testExecute execute Success...");
			XxlJobHelper.handleSuccess("testExecute执行成功参数：" + param);
		} catch (Exception e) {
			log.error("testExecute execute Fail ...", e);
			XxlJobHelper.log("testExecute execute Fail...");
			XxlJobHelper.handleFail("testExecute执行失败参数：" + param + ",错误：" + e.getMessage());
		}
	}

	@XxlJob("weekJobBuild")
	public void weekJobBuild() throws Exception {

		try {
			ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
			RequestContextHolder.setRequestAttributes(servletRequestAttributes, true);
			LocalDateTime now = LocalDateTime.now();
			DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
			log.info("LocalDateTime:" + dtf.format(now));
			XxlJobHelper.log("LocalDateTime:" + dtf.format(now));
			//周报任务建立-抓取人员信息
			weekJobBuildExec(null);
			XxlJobHelper.log("weekJobBuild 执行成功");
			XxlJobHelper.handleSuccess("weekJobBuild执行成功");
		} catch (Exception e) {
			e.printStackTrace();
			log.error("weekJobBuild 执行失败", e);
			XxlJobHelper.log("weekJobBuild 执行失败 ...");
			XxlJobHelper.handleFail("weekJobBuild  执行失败 " + "，错误：" + e.getMessage());
		}
	}

	public void weekJobBuildExec(String weekDate) throws Exception {

		try {
			ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
			RequestContextHolder.setRequestAttributes(servletRequestAttributes, true);
			LocalDateTime now = LocalDateTime.now();
			DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
			log.info("LocalDateTime:" + dtf.format(now));
			XxlJobHelper.log("LocalDateTime:" + dtf.format(now));
			//周报任务建立-抓取人员信息
			homeworkFeignService.weekJobBuild(weekDate, SecurityConstants.FROM_IN);

			XxlJobHelper.log("weekJobBuild 执行成功");
			XxlJobHelper.handleSuccess("weekJobBuild执行成功，参数：" + weekDate);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("weekJobBuild 执行失败", e);
			XxlJobHelper.log("weekJobBuild 执行失败 ...");
			XxlJobHelper.handleFail("weekJobBuild执行失败参数：" + weekDate + ",错误：" + e.getMessage());
		}
	}

	@XxlJob("weekSendMember")
	public void weekSendMember() throws Exception {
		String param = XxlJobHelper.getJobParam();
		log.info("接收调度中心参数...[{}]", param);
		log.info("weekSendMember execute...");

		try {
			LocalDateTime now = LocalDateTime.now();
			DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
			log.info("LocalDateTime:" + dtf.format(now));
			XxlJobHelper.log("LocalDateTime:" + dtf.format(now));
			//周报任务建立-抓取人员信息
			homeworkFeignService.weekSendMember(SecurityConstants.FROM_IN);

			XxlJobHelper.log("weekSendMember 执行成功");
			XxlJobHelper.handleSuccess("weekJobBuild执行成功，参数：" + param);
		} catch (Exception e) {
			log.error("weekSendMember 执行失败", e);
			XxlJobHelper.log("weekSendMember 执行失败 ...");
			XxlJobHelper.handleFail("weekSendMember执行失败参数：" + param + ",错误：" + e.getMessage());
		}
	}

	@XxlJob("batchStarRank")
	public void batchStarRank() throws Exception {
		R r = musicRemoteService.batchStarRank(SecurityConstants.FROM_IN);
	}

	@XxlJob("weekJobRestart")
	public void weekJobRestart() throws Exception {
		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		RequestContextHolder.setRequestAttributes(servletRequestAttributes, true);
		String weekDate = XxlJobHelper.getJobParam();
		log.info("接收调度中心参数...[{}]", weekDate);
		log.info("weekJobBuild execute...");
		//TODO 指定构建某一周
		weekJobBuildExec(weekDate);
	}

}
